<div class="layui-card">

    <div class="layui-card-body">
        <div class="layui-form toolbar">
            <button id="stage-iter-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>
        <table class="layui-table" id="stage-iter-table" lay-filter="stage-iter-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="stage-iter-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="stage-iter-tpl-state">
    <input type="checkbox" lay-filter="stage-iter-tpl-state" value="{{d.stitid}}" lay-skin="switch" lay-text="使用|禁用"
           {{d.status==1?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin','layer'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 添加按钮绑定权限
        /*if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-add').hide();
        }*/

        // 导出按钮绑定权限
        /*if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-export').hide();
        }*/
        var sicode = admin.getTempData('t_stage_iter');

        // 渲染表格
        table.render({
            elem: '#stage-iter-table',
            url: config.base_server + 'api-study/stageIter/getList',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token,
                sicode: sicode
            },
            page: true,
            cols: [[
                // {type: 'numbers'},
                {field: 'conten', title: '迭代内容',edit: 'text'},
                {field: 'iorder', sort: true, title: '排序',edit: 'text',width: 150},
                {field: 'cretim', sort: true, title: '创建时间'},
                {field: 'status', templet: '#stage-iter-tpl-state', title: '状态', width: 150}
            ]]
        });
        //监听单元格编辑
        table.on('edit(stage-iter-table)',function (obj){
            var data = obj.data //得到所在行所有键值
            $.ajax({
                url: config.base_server + 'api-study/stageIter/update',
                data: JSON.stringify(data),
                type: "post",
                dataType: 'json',
                contentType: 'application/json',
                // processData: false,
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg(data.msg, {icon: 1, time: 1000});
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 1000});
                    }
                },
                beforeSend: function (xhr) {
                    var token = config.getToken();
                    if (token) {
                        xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                    }
                }
            });
        });
        // 添加按钮点击事件
        $('#stage-iter-btn-add').click(function () {
            admin.putTempData('t_stage_iter',sicode);
            admin.popupCenter2({
                title: '添加迭代阶段',
                path: 'pages/study/stageIter_form.html',
                finish: function () {
                    table.reload('stage-iter-table',{where:{sicode: sicode}});
                }
            });
        });

        //显示表单弹窗
        var showEditModel = function () {

        };

        // 修改状态
        form.on('switch(stage-iter-tpl-state)', function (obj) {
            layer.load(2);
            var data = new FormData();
            data.append("stitid", obj.elem.value);
            data.append("status", obj.elem.checked ? 1 : 2);
            $.ajax({
                url: config.base_server + 'api-study/stageIter/delete',
                data: data,
                type: "post",
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function (data) {
                    layer.closeAll('loading');
                    if (data.code === 0) {
                        layer.msg(data.msg, {icon: 1, time: 1000});
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 1000});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                },
                beforeSend: function (xhr) {
                    var token = config.getToken();
                    if (token) {
                        xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                    }
                }
            });
        })
        // 工具条点击事件
        /*

         // 导出
         $('#user-btn-export').click(function () {
             var url = config.base_server + 'api-user/users/exportUser?access_token='+config.getToken().access_token;
             var xhr = new XMLHttpRequest();
             xhr.open('POST', url, true);
             xhr.responseType = "blob";
             xhr.setRequestHeader("client_type", "DESKTOP_WEB");
             xhr.onload = function() {
                 if (this.status == 200) {
                     var blob = this.response;
                     var objecturl = URL.createObjectURL(blob);
                     window.location.href = objecturl;
                 }
             }
             xhr.send();
         });*/


    });
</script>